package util;

import java.io.FileNotFoundException;
import java.io.PrintStream;

import data.GrapData;

public class WriteMap {
	private String fileName;
	private PrintStream fileStream;
	
	private GrapData grapData;

	public WriteMap(String fileName, GrapData grapData) {
		this.fileName = fileName;
		this.grapData = grapData;
	}

	private void writeHeader() {
		try {
			fileStream = new PrintStream(fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String startStr = "";
		String endStr = "";
		if(grapData.getStart() != null) {
			startStr = grapData.getStart() .getX() + "," + grapData.getStart().getY(); 
		}
		if(grapData.getEnd() != null) {
			endStr = grapData.getEnd().getX() + "," + grapData.getEnd().getY();
		}
		fileStream.println(startStr);
		fileStream.println(endStr);
		
		fileStream.println(grapData.getrows());
		fileStream.println(grapData.getcols());
	}

	private void writeMatrix() {
		for (int[] array : grapData.getGrap()) {
			for (int ele : array) {
				fileStream.print(ele + " ");
			}
			fileStream.println();
		}
	}

	private void close() {
		fileStream.flush();
		fileStream.close();
	}
	
	public void write() {
		writeHeader();
		writeMatrix();
		close();
	}

}
